package com.yangshun.wiki.service;

import com.yangshun.wiki.domain.Ebook;
import com.yangshun.wiki.domain.EbookExample;
import com.yangshun.wiki.mapper.EbookMapper;
import com.yangshun.wiki.req.EbookReq;
import com.yangshun.wiki.resp.EbookResp;
import com.yangshun.wiki.util.CopyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping
public class EbookService {

    @Resource
    private EbookMapper ebookMapper;


    public List<EbookResp>list(EbookReq req) {
        EbookExample ebookExample = new EbookExample();
        EbookExample.Criteria criteria = ebookExample.createCriteria();
        if(!ObjectUtils.isEmpty(req.getName())) {
            criteria.andNameLike("%" + req.getName() + "%");
        }
        List<Ebook> ebookList = ebookMapper.selectByExample(ebookExample);

        List<EbookResp> respList = new ArrayList<>();
        for (Ebook ebook : ebookList) {
            //EbookResp ebookResp = new EbookResp();
            //因为不想一个个的写get和set，所以这个方法被放弃了
            //ebookResp.setId(ebook.getId());
            //BeanUtils.copyProperties(ebook,ebookResp);
            EbookResp ebookResp = CopyUtil.copy(ebook,EbookResp.class);
            respList.add(ebookResp);
        }

        List<EbookResp> list = CopyUtil.copyList(ebookList,EbookResp.class);
        return list;
    }
}
